package osb.map.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import osb.map.publicdata.Public_Operate_And_Data;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;
import com.ljp.ani.R;

/**
 * 地图标志类 
 */

public class Map_ItemizedOverlay extends ItemizedOverlay<OverlayItem>
{

	public  List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
	private Drawable marker;
	private Context mContext;
	static Map_ItemizedOverlay overitem ;
	
	static Map_ItemizedOverlay Static_Map_ItemizedOverlay ;
	

	public static void ShowMap_ItemizedOverlay(Context context, Map_Entity map_Entity)
	{
		if (context == null) // 默认对象
		{
			context = Map.Public_Static_Map;
		}
		// 删除原图层
		if(map_Entity.map_ItemizedOverlay != null)
		Map.Public_Static_Map.mapView.getOverlays().remove(map_Entity.map_ItemizedOverlay);
		map_Entity.map_ItemizedOverlay = new Map_ItemizedOverlay(map_Entity.drawable, context, map_Entity.geoPoint,"","");
		Map.Public_Static_Map.mapView.getOverlays().add(map_Entity.map_ItemizedOverlay);	
	}

	public static void RemoveMap_ItemizedOverlay(Map_Entity map_Entity)
	{
		// 删除原图层
		if(map_Entity.map_ItemizedOverlay != null)
		Map.Public_Static_Map.mapView.getOverlays().remove(map_Entity.map_ItemizedOverlay);	
	}
	
	public Map_ItemizedOverlay(Drawable marker, Context context,GeoPoint geopoint, String lineString, String detailsString)
	{
		super(boundCenterBottom(marker));
		Static_Map_ItemizedOverlay=this;
		this.marker = marker;
		this.mContext = context;
//		this.marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); // 为maker定义位置和边界
		mGeoList.add(new OverlayItem(geopoint,lineString,detailsString));
		populate(); 
		// createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
	}

	/**
	 * 清除所有小绿图层
	 */
	public static void CloseGetxiaolvlocation() 
	{
		Iterator mapite=Map_Entity.static_Map_Entity.entrySet().iterator();
        while(mapite.hasNext())  
        {  
            HashMap.Entry testDemo=(HashMap.Entry)mapite.next();  
            Object key=testDemo.getKey();  
            Object value=testDemo.getValue();  
            

            Map_Entity map_Entity = (Map_Entity) value;
            if(map_Entity.map_ItemizedOverlay != null)
    		Map.Public_Static_Map.mapView.getOverlays().remove(map_Entity.map_ItemizedOverlay);
        }
        Map.Public_Static_Map.mapView.invalidate();
	}
	
	public void updateOverlay()
	{
		populate();
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when)
	{
		// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
				Projection projection = mapView.getProjection();
				for (int index = size() - 1; index >= 0; index--)
				{ 
					// 遍历mGeoList
					OverlayItem overLayItem = getItem(index); // 得到给定索引的item

//					String title = overLayItem.getTitle();
//					Point point = projection.toPixels(overLayItem.getPoint(), null);

//					Paint paintText = new Paint();
//					paintText.setColor(Color.BLUE);
//					paintText.setTextSize(15);
//					canvas.drawText(title, point.x - 30, point.y, paintText); // 绘制文本
				}
		return super.draw(canvas, mapView, shadow, when);
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow)
	{
		
		super.draw(canvas, mapView, shadow);
	}

	@Override
	protected OverlayItem createItem(int i)
	{
		return mGeoList.get(i);
	}

	@Override
	public int size()
	{
		return mGeoList.size();
	}

}
